//
//  MyBoundingButton.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/10/13.
//
//

#include "MyBouncingButton.h"


bool MyBouncingButton::init(){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    velocity = winSize.width/(320*2);
    originalVelocity = velocity;
    return true;
}


MyBouncingButton* MyBouncingButton::create(const char *fileName, double mX, double mY, double mWidth, double mHeight){
    MyBouncingButton* button = MyBouncingButton::create();
    button->setPosition(ccp(mX,mY));
    button->buttonSize = CCSizeMake(mWidth, mHeight);
    button->sprite = CCSprite::create(fileName);
    button->sprite->setAnchorPoint(ccp(0.5,0.5));
    button->sprite->setScaleX(mWidth/button->sprite->getContentSize().width);
    button->sprite->setScaleY(mHeight/button->sprite->getContentSize().height);
    button->sprite->setPosition(ccp(mWidth/2,mHeight/2));
    button->addChild(button->sprite);

    
    
    
    
    return button;
}

void MyBouncingButton::goFaster(){
    velocity+=(originalVelocity/2);
}

void MyBouncingButton::addLimitRect(cocos2d::CCRect mLimitRect){
    limitRect = mLimitRect;
}

void MyBouncingButton::pushToMoveToRandomDirection(){
    signX = rand()% 2;
    (signX == 0) ? signX = -1 : signX = 1;
    
    signY = rand()% 2;
    (signY == 0) ? signY = -1 : signY = 1;
}
bool MyBouncingButton::isOnTheSameDiretionWith(MyBouncingButton *otherButton){
    return ((signX==otherButton->signX)&&(signY==otherButton->signY));
}

void MyBouncingButton::handleUpdate(float dt){
    this->setPosition(ccp(getPositionX()+signX*velocity,getPositionY()+signY*velocity));
    if(getPositionX()<=limitRect.origin.x || getPositionX()+buttonSize.width>=limitRect.origin.x+limitRect.size.width){
        signX = -signX;
    }
    if(getPositionY()<=limitRect.origin.y || getPositionY()+buttonSize.height >= limitRect.origin.y+limitRect.size.height){
        signY = -signY;
    }
    
}